package service

import (
	"context"
	"github.com/kuangshp/go-utils/k"
	"github.com/spf13/cast"
	"insighttracking/common/utils"
	"insighttracking/query/bo"
	"insighttracking/query/dao"
	"insighttracking/query/model"
)

type _purchaseOrderService struct {
}

func (s _purchaseOrderService) CreateApi(ctx context.Context, tx *dao.QueryTx, data *model.PurchaseOrdersEntity) error {
	if err := tx.PurchaseOrdersEntity.WithContext(ctx).Create(data); err != nil {
		return err
	}
	return nil
}

func (s _purchaseOrderService) GetPurchaseOrderPageApi(ctx context.Context, enterpriseId int64, req bo.GetPurchaseOrderPageReqBo) ([]bo.GetPurchaseOrderPageRespBo, int64, error) {
	tx := dao.PurchaseOrdersEntity.WithContext(ctx).Where(dao.PurchaseOrdersEntity.EnterpriseID.Eq(enterpriseId))
	if req.SupplierID != "" {
		tx = tx.Where(dao.PurchaseOrdersEntity.SupplierID.Eq(cast.ToInt64(req.SupplierID)))
	}
	if req.PurchaserID != "" {
		tx = tx.Where(dao.PurchaseOrdersEntity.PurchaserID.Eq(cast.ToInt64(req.PurchaserID)))
	}
	if req.Types != "" {
		tx = tx.Where(dao.PurchaseOrdersEntity.Types.Eq(cast.ToInt64(req.Types)))
	}
	if req.OrderNo != "" {
		tx = tx.Where(dao.PurchaseOrdersEntity.OrderNo.Eq(req.OrderNo))
	}
	if req.ExpectationGoodsDate != "" {
		tx = tx.Where(dao.PurchaseOrdersEntity.ExpectationGoodsDate.Eq(utils.DateTimeStrToTime(req.ExpectationGoodsDate)))
	}
	if req.StartDate != "" && req.EndDate != "" {
		tx = tx.Where(dao.PurchaseOrdersEntity.CreatedAt.Between(utils.DateTimeStrToTime(req.StartDate), utils.DateTimeStrToTime(req.EndDate)))
	}
	if req.StoreStatus != "" {
		tx = tx.Where(dao.PurchaseOrdersEntity.StoreStatus.Eq(cast.ToInt64(req.StoreStatus)))
	}
	if req.Source != "" {
		tx = tx.Where(dao.PurchaseOrdersEntity.Source.Eq(cast.ToInt64(req.Source)))
	}
	if req.Status != "" {
		tx = tx.Where(dao.PurchaseOrdersEntity.Status.Eq(cast.ToInt64(req.Status)))
	}
	offset, limit := utils.DbPage(req.PageNumber, req.PageSize)
	var getPurchaseOrderPageRespBo = make([]bo.GetPurchaseOrderPageRespBo, 0)
	count, err := tx.
		Select(
			dao.PurchaseOrdersEntity.ALL,
			dao.EnterprisePersonEntity.Name.As("PurchaserName"),
			dao.SupplierEntity.Name.As("SupplierName"),
		).
		LeftJoin(dao.EnterprisePersonEntity, dao.EnterprisePersonEntity.ID.EqCol(dao.PurchaseOrdersEntity.PurchaserID)).
		LeftJoin(dao.SupplierEntity, dao.SupplierEntity.ID.EqCol(dao.PurchaseOrdersEntity.SupplierID)).
		Order(dao.PurchaseOrdersEntity.ID.Desc()).
		ScanByPage(&getPurchaseOrderPageRespBo, offset, limit)
	return getPurchaseOrderPageRespBo, count, err
}

func (s _purchaseOrderService) GetPurchaseOrderByIdApi(ctx context.Context, enterpriseId int64, id int64) (*bo.GetPurchaseOrderPageRespBo, error) {
	tx := dao.PurchaseOrdersEntity.WithContext(ctx).Where(
		dao.PurchaseOrdersEntity.EnterpriseID.Eq(enterpriseId),
		dao.PurchaseOrdersEntity.ID.Eq(id),
	)
	var getPurchaseOrderPageRespBo = bo.GetPurchaseOrderPageRespBo{}
	err := tx.
		Select(
			dao.PurchaseOrdersEntity.ALL,
			dao.EnterprisePersonEntity.Name.As("PurchaserName"),
			dao.SupplierEntity.Name.As("SupplierName"),
		).
		LeftJoin(dao.EnterprisePersonEntity, dao.EnterprisePersonEntity.ID.EqCol(dao.PurchaseOrdersEntity.PurchaserID)).
		LeftJoin(dao.SupplierEntity, dao.SupplierEntity.ID.EqCol(dao.PurchaseOrdersEntity.SupplierID)).
		Scan(&getPurchaseOrderPageRespBo)
	return &getPurchaseOrderPageRespBo, err
}

func (s _purchaseOrderService) ModifyStatusByIdApi(ctx context.Context, id int64, status int64) error {
	if _, err := dao.PurchaseOrdersEntity.WithContext(ctx).Where(dao.PurchaseOrdersEntity.ID.Eq(id)).UpdateSimple(
		dao.PurchaseOrdersEntity.Status.Value(status),
	); err != nil {
		return err
	}
	return nil
}

// GetPurchaseOrderStatusByOrderIdApi  根据订单id查询采购订单状态
func (s _purchaseOrderService) GetPurchaseOrderStatusByOrderIdApi(ctx context.Context, orderId int64) (inStatus int64) {
	// 1.查询全部的采购计划
	detailEntities, err := OrdersDetailService.GetByOrderIdListApi(ctx, []int64{orderId})
	if err == nil && len(detailEntities) > 0 {
		orderDetailIdList := k.Map(detailEntities, func(item *model.OrdersDetailEntity, index int) int64 {
			return item.ID
		})
		// 2.查询采购计划
		planOrdersEntities, err := PurchasePlanOrdersService.GetByOrdersDetailIdListApi(ctx, orderDetailIdList)
		if err == nil && len(planOrdersEntities) > 0 {
			planIdList := k.Map(planOrdersEntities, func(item *model.PurchasePlanOrdersEntity, index int) int64 {
				return item.PurchasePlanID
			})
			purchaseOrdersDetailEntities, err := PurchaseOrderDetailService.GetByPurchasePlanIdListApi(ctx, planIdList)
			if err == nil && len(purchaseOrdersDetailEntities) > 0 {
				isOk := k.Every(purchaseOrdersDetailEntities, func(item *model.PurchaseOrdersDetailEntity, index int) bool {
					return item.StoreStatus == 10
				})
				if isOk {
					inStatus = 10
				}
			}
		}
	}
	return
}

var PurchaseOrderService = new(_purchaseOrderService)
